<template>
  <div>
    <el-form :inline="true" :model="cFrom" class="demo-form-inline" :disabled='!isShowList'>
      <el-form-item label="一级分类">
        <el-select
          v-model="cFrom.category1Id"
          placeholder="请选择"
          @change="c1Change"
        >
          <el-option
            :label="c1.name"
            v-for="c1 in category1List"
            :key="c1.id"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="cFrom.category2Id"
          placeholder="请选择"
          @change="c2Change"
        >
          <el-option
            :label="c2.name"
            v-for="c2 in category2List"
            :key="c2.id"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          v-model="cFrom.category3Id"
          placeholder="请选择"
          @change="c3Change"
        >
          <el-option
            :label="c3.name"
            v-for="c3 in category3List"
            :key="c3.id"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelector",
  data() {
    return {
      cFrom: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
      category1List: [],
      category2List: [],
      category3List: [],
    };
  },
  props:{
    isShowList:Boolean
  },
  mounted() {
    this.getCategory1();
  },
  methods: {
    async getCategory1() {
      const { data } = await this.$API.category.getCategory1();
      this.category1List = data;
    },
    async getCategory2() {
      // const { data } = await this.$API.category.getCategory2(
      //   this.cFrom.category1Id
      // );
      // this.category2List = data;
    },
    async getCategory3() {
      const { data } = await this.$API.category.getCategory3(
        this.cFrom.category2Id
      );
      this.category3List = data;
    },
    // 当用户选中一级分类时触发
    async c1Change() {
      // 通过接口更新category2List的数据
      // this.getCategory2();
      const { data } = await this.$API.category.getCategory2(
        this.cFrom.category1Id
      );
      this.category2List = data;
      // 重新选择的时候清空后代的数据
      this.cFrom.category2Id = "";
      this.cFrom.category3Id = "";
      // this.category2List = [];
      this.category3List = [];
    },
    // 当用户选中二级分类时触发
    c2Change() {
      this.getCategory3();
      // 重新选择的时候清空后代的数据
      this.cFrom.category3Id = "";
      this.category3List = [];
    },
    c3Change() {
      this.$emit("changeCategory", {
        category1Id: this.cFrom.category1Id,
        category2Id: this.cFrom.category2Id,
        category3Id: this.cFrom.category3Id,
      });
    },
  },
};
</script>

<style>
</style>
